﻿using Enums;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using XF.Common;
using XF.ExControls;

namespace KYBM
{
    public partial class FrmSTOPrint : Form
    {
        private XF.BLL.Bill_STOItems bll = new XF.BLL.Bill_STOItems();
        private DateTimePicker dtpDate = new DateTimePicker();
        private List<int> lstUpdate = new List<int>();
        private DataTable dtDataSource = new DataTable();

        public FrmSTOPrint()
        {
            InitializeComponent();
        }

        private void FrmFrmSTOPrint_Load(object sender, EventArgs e)
        {
            xfDataGridView1.AutoGenerateColumns = false;
            dtpDate.Width = 120;
            dtpDate.Format = DateTimePickerFormat.Custom;
            dtpDate.CustomFormat = "yyyy-MM-dd";
            ToolStripControlHost item = new ToolStripControlHost(dtpDate);
            toolStrip1.Items.Insert(4, item);
            ColUnit.DisplayMember = MessageText.LISTITEM_TEXT;
            ColUnit.ValueMember = MessageText.LISTITEM_VALUE;
            ColUnit.DataSource = GenerateData.GetUnit(false);
            BindData();
        }

        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            string sWhere = " CONVERT(varchar(10), OrderDate, 120) = '" + dtpDate.Value.ToString("yyyy-MM-dd") + "' and Status <> 1";
            if (!string.IsNullOrEmpty(tsTbSupplier.Text))
            {
                sWhere += " and Supplier like '%" + tsTbSupplier.Text + "%'";
            }
            dtDataSource = bll.GetDetailList(sWhere).Tables[0];
            List<XF.Model.Bill_STOItems> models = bll.DataTableToList(dtDataSource);
            xfDataGridView1.Rows.Clear();
            foreach(XF.Model.Bill_STOItems model in models)
            {
                int count = xfDataGridView1.RowCount;
                xfDataGridView1.Rows.Add();
                xfDataGridView1.Rows[count].Cells[ColOrderId.Name].Value = model.OrderId;
                xfDataGridView1.Rows[count].Cells[ColCustomerId.Name].Value = model.CustomerId;
                xfDataGridView1.Rows[count].Cells[ColCustomerCode.Name].Value = model.CustomerCode;
                xfDataGridView1.Rows[count].Cells[ColCustomerName.Name].Value = model.CustomerName;
                xfDataGridView1.Rows[count].Cells[ColId.Name].Value = model.Id;
                xfDataGridView1.Rows[count].Cells[ColItemCode.Name].Value = model.ItemCode;
                xfDataGridView1.Rows[count].Cells[ColMaterielId.Name].Value = model.MaterielId;
                xfDataGridView1.Rows[count].Cells[ColMaterielName.Name].Value = model.MaterielName;
                xfDataGridView1.Rows[count].Cells[ColMaterielModel.Name].Value = model.MaterielModel;
                xfDataGridView1.Rows[count].Cells[ColUnit.Name].Value = model.Unit;
                xfDataGridView1.Rows[count].Cells[ColQty.Name].Value = model.Qty;
                xfDataGridView1.Rows[count].Cells[ColReamrk.Name].Value = model.Remark;
                xfDataGridView1.Rows[count].Cells[ColSupplier.Name].Value = model.Supplier;
            }
            lstUpdate.Clear();
        }

        private void tsBtnSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }

        /// <summary>
        /// 数据保存
        /// </summary>
        private void SaveData()
        {
            string msg = string.Empty;
            xfDataGridView1.EndEdit();
            if (lstUpdate.Count == 0)
            {
                BindData();
            }
            else
            {
                msg += UpdateData();
                if (msg.Equals(string.Empty))
                {
                    QQMessageBox.Show(
                    this,
                    MessageText.TIP_SUCCESS_SAVE,
                    MessageText.MESSAGEBOX_CAPTION_TIP,
                    QQMessageBoxIcon.OK,
                    QQMessageBoxButtons.OK);
                    BindData();
                }
                else
                {
                    QQMessageBox.Show(
                    this,
                    msg,
                    MessageText.MESSAGEBOX_CAPTION_ERROR,
                    QQMessageBoxIcon.Error,
                    QQMessageBoxButtons.OK);
                }
            }
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <returns></returns>
        private string UpdateData()
        {
            string result = string.Empty;
            List<int> successRowIndex = new List<int>();
            foreach (int index in lstUpdate)
            {
                int iId = zDataConverter.ToInt(xfDataGridView1.Rows[index].Cells[ColId.Name].Value);
                string sItemCode = zDataConverter.ToString(xfDataGridView1.Rows[index].Cells[ColItemCode.Name].Value);
                if (bll.SaveItemCode(iId, sItemCode))
                {
                    successRowIndex.Add(index);
                }
                else
                {
                    result += MessageText.SQL_ERROR_UPDATE + MessageText.KEY_ENTER;
                }
            }
            foreach (int rowIndex in successRowIndex)
            {
                lstUpdate.Remove(rowIndex);
            }
            return result;
        }

        private void xfDataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (!lstUpdate.Contains(e.RowIndex))
            {
                lstUpdate.Add(e.RowIndex);
            }
        }

        private void tsBtnPrint_Click(object sender, EventArgs e)
        {
            FrmSTOReport frmSTOReport = new FrmSTOReport();
            frmSTOReport.Sourse = dtDataSource;
            frmSTOReport.ShowDialog();
        }
    }
}
